Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Public Class oPrint
    Dim S_BillCode As Integer
    Dim S_FilePath As String
    Dim S_PrinterName As String
    Dim S_IsShow As Boolean
    Dim S_Count As Integer = 1

    Dim K_BillCode As Integer
    Dim K_FilePath As String
    Dim K_PrinterName As String
    Dim K_IsShow As Boolean
    Dim K_Count As Integer = 1

    Public Sub PrintSlip(ByVal BillCode As Int64, ByVal IsShow As Boolean, ByVal FilePath As String, ByVal PrinterName As String, ByVal Count As Integer)
        S_BillCode = BillCode
        S_FilePath = FilePath
        S_PrinterName = PrinterName
        S_IsShow = IsShow
        S_Count = Count
        Dim th As New Threading.Thread(AddressOf Thread_PrintSlip)
        th.IsBackground = True
        th.Start()
    End Sub
    Public Shared Sub PRINT_KITCHEN_ADD(ByVal BILL_CODE As Int64, _
    ByVal ITEM_CODE As String, ByVal ITEM_NAME As String, ByVal QUANTITY As Double, _
    ByVal KEEP_PACK As String, ByVal MEMO As String)
        Try
            If PublicFunction.OpenSQLConnection() Then
                Dim MyCommand As SqlCommand = New SqlCommand("PRINT_KITCHEN_ADD", PublicVariable.Connection)
                MyCommand.CommandType = CommandType.StoredProcedure
                MyCommand.Parameters.AddWithValue("@BILL_CODE", BILL_CODE)
                MyCommand.Parameters.AddWithValue("@ITEM_CODE", ITEM_CODE)
                MyCommand.Parameters.AddWithValue("@ITEM_NAME", ITEM_NAME)
                MyCommand.Parameters.AddWithValue("@QUANTITY", QUANTITY)
                MyCommand.Parameters.AddWithValue("@KEEP_PACK", KEEP_PACK)
                MyCommand.Parameters.AddWithValue("@MEMO", MEMO)
                MyCommand.ExecuteNonQuery()
                PublicVariable.Connection.Close()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Shared Sub PRINT_KITCHEN_FINISH(ByVal BILL_CODE As Int64, _
    ByVal PRINTER_NAME As String)
        Try
            If PublicFunction.OpenSQLConnection() Then
                Dim MyCommand As SqlCommand = New SqlCommand("PRINT_KITCHEN_FINISH", PublicVariable.Connection)
                MyCommand.CommandType = CommandType.StoredProcedure
                MyCommand.Parameters.AddWithValue("@BILL_CODE", BILL_CODE)
                MyCommand.Parameters.AddWithValue("@PRINTER_NAME", PRINTER_NAME)
                MyCommand.ExecuteNonQuery()
                PublicVariable.Connection.Close()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub Print_KITCHEN(ByVal BillCode As Int64, _
    ByVal IsShow As Boolean, ByVal FilePath As String, _
    ByVal PrinterName As String)
        K_BillCode = BillCode
        K_FilePath = FilePath
        K_PrinterName = PrinterName
        K_IsShow = IsShow
        Dim th As New Threading.Thread(AddressOf Thread_PrintKitchen)
        th.IsBackground = True
        th.Start()
    End Sub
    Public Shared Function LOAD_PRINT_KITCHEN(ByVal BILL_CODE As Int64) As DataTable
        Try
            If PublicFunction.OpenSQLConnection() Then
                Dim MyCommand As SqlCommand = New SqlCommand("PRINT_KITCHEN_LOADBYBILL", PublicVariable.Connection)
                MyCommand.CommandType = CommandType.StoredProcedure
                MyCommand.Parameters.AddWithValue("@BILL_CODE", BILL_CODE)
                Dim da As SqlDataAdapter = New SqlDataAdapter()
                Dim mytable As DataTable = New DataTable()
                da.SelectCommand = MyCommand
                da.Fill(mytable)
                PublicVariable.Connection.Close()
                Return mytable
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Function
    Private Sub Thread_PrintKitchen()
        Dim fReportViewer As New frmReport
        Dim pvCollection As New CrystalDecisions.Shared.ParameterValues
        Dim BILL_CODE As New CrystalDecisions.Shared.ParameterDiscreteValue
        Dim PRINTER_NAME As New CrystalDecisions.Shared.ParameterDiscreteValue

        Dim ConStr As String = PublicVariable.ReportConnectionString

        Dim MyArr As Array = ConStr.Split(";")
        ' Create a report document instance to hold the report

        Dim crtableLogoninfos As New TableLogOnInfos
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim crConnectionInfo As New ConnectionInfo
        Dim CrTables As Tables
        Dim CrTable As Table
        Try
            ' Load the report 
            Dim rptRedInvoice As New ReportDocument
            rptRedInvoice.Load(K_FilePath)

            'Set DB con
            With crConnectionInfo
                .ServerName = MyArr(0)
                .DatabaseName = MyArr(1)
                .UserID = MyArr(2)
                .Password = MyArr(3)
            End With

            'Apply DB con
            CrTables = rptRedInvoice.Database.Tables

            For Each CrTable In CrTables
                crtableLogoninfo = CrTable.LogOnInfo
                crtableLogoninfo.ConnectionInfo = crConnectionInfo
                CrTable.ApplyLogOnInfo(crtableLogoninfo)
                CrTable.Location = CrTable.Location.Substring(CrTable.Location.LastIndexOf(".") + 1)

            Next
            BILL_CODE.Value = K_BillCode
            pvCollection.Add(BILL_CODE)
            rptRedInvoice.DataDefinition.ParameterFields("@BILL_CODE").ApplyCurrentValues(pvCollection)

            PRINTER_NAME.Value = K_PrinterName
            pvCollection.Add(PRINTER_NAME)
            rptRedInvoice.DataDefinition.ParameterFields("@PRINTER").ApplyCurrentValues(pvCollection)

            Dim doctoprint As New System.Drawing.Printing.PrintDocument()
            doctoprint.PrinterSettings.PrinterName = K_PrinterName

            fReportViewer.CrystalReportViewer1.ReportSource = rptRedInvoice
            rptRedInvoice.PrintOptions.PrinterName = K_PrinterName
            rptRedInvoice.PrintOptions.PaperSize = PaperSize.DefaultPaperSize
            If K_IsShow Then
                fReportViewer.showForm(rptRedInvoice)
            Else
                rptRedInvoice.PrintToPrinter(1, False, 0, 0)
            End If
            PRINT_KITCHEN_FINISH(K_BillCode, K_PrinterName)
            K_BillCode = 0
            K_FilePath = ""
            K_PrinterName = ""
            K_IsShow = False
        Catch Exp As Exception
            MessageBox.Show(Exp.Message)
        End Try
    End Sub
    Private Sub Thread_PrintSlip()
        Dim fReportViewer As New frmReport
        Dim pvCollection As New CrystalDecisions.Shared.ParameterValues
        Dim DocEntryPara As New CrystalDecisions.Shared.ParameterDiscreteValue
        Dim ConStr As String = PublicVariable.ReportConnectionString

        Dim MyArr As Array = ConStr.Split(";")
        ' Create a report document instance to hold the report

        Dim crtableLogoninfos As New TableLogOnInfos
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim crConnectionInfo As New ConnectionInfo
        Dim CrTables As Tables
        Dim CrTable As Table
        Try
            ' Load the report 
            Dim rptRedInvoice As New ReportDocument
            rptRedInvoice.Load(S_FilePath)

            'Set DB con
            With crConnectionInfo
                .ServerName = MyArr(0)
                .DatabaseName = MyArr(1)
                .UserID = MyArr(2)
                .Password = MyArr(3)
            End With

            'Apply DB con
            CrTables = rptRedInvoice.Database.Tables

            For Each CrTable In CrTables
                crtableLogoninfo = CrTable.LogOnInfo
                crtableLogoninfo.ConnectionInfo = crConnectionInfo
                CrTable.ApplyLogOnInfo(crtableLogoninfo)
                CrTable.Location = CrTable.Location.Substring(CrTable.Location.LastIndexOf(".") + 1)

            Next
            DocEntryPara.Value = S_BillCode
            pvCollection.Add(DocEntryPara)
            rptRedInvoice.DataDefinition.ParameterFields("@BillCode").ApplyCurrentValues(pvCollection)

            Dim doctoprint As New System.Drawing.Printing.PrintDocument()
            doctoprint.PrinterSettings.PrinterName = S_PrinterName
           
            fReportViewer.CrystalReportViewer1.ReportSource = rptRedInvoice
            rptRedInvoice.PrintOptions.PrinterName = S_PrinterName
            rptRedInvoice.PrintOptions.PaperSize = PaperSize.DefaultPaperSize
            If S_IsShow Then
                fReportViewer.showForm(rptRedInvoice)
            Else
                For i As Integer = 0 To S_Count - 1
                    rptRedInvoice.PrintToPrinter(1, False, 0, 0)
                Next
            End If
            S_BillCode = 0
            S_FilePath = ""
            S_PrinterName = ""
            S_IsShow = False
            S_Count = 1
        Catch Exp As Exception
            MessageBox.Show(Exp.Message)
        End Try
    End Sub
End Class
